/**
 * Created by yongwang on 2015/9/2.
 * 分页
 */
(function(w){
    var pagination = function(data,suf){
        this.data = data;
        this.currentPage = data.currentPage;
        this.pageSize = data.pageSize;
        this.idSuffix = (!suf) ? '':suf;
        this.init();
    }, c,cPage = 0;
    pagination.prototype = {
        init: function(){
            c = this;
            cPage = c.currentPage;
            c.evt();
            this.selectPage();
        },
        selectPage: function(pageNumber){
            if(pageNumber)c.currentPage = pageNumber;
            c.data.ajax.post(c.data.url.replace(/:page/,c.currentPage).replace(/:size/,c.pageSize), c.searchParams?c.searchParams:c.data.params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if($("#page_header"+ this.idSuffix).length == 0){
                    c.totalPage = data.data.totalPage;
                    //c.pageHeader(c.totalPage);
                }
                c.data.success(data);
            }).error(function (err) {
                c.data.error(err);
            });
        },
        pageHeader: function(totalPage){//表头部
            $("#pagination"+ this.idSuffix)[0].innerHTML = "";
            var html = "<ul class='am-pagination left-margin' id = 'page_header"+ this.idSuffix+"'><li id = 'first_page"+ this.idSuffix+"'><a href='javascript:'>首页</a></li><li class='am-disabled' id = 'pre_page"+ this.idSuffix+"'><a href='javascript:'>&laquo;</a></li>";
            //html += "<li><div></div></li>";
            var current = Number(c.currentPage);
            var total = Number(c.totalPage);

            for(var i = 1;i <= totalPage;i++){
                if(i == current){
                    html += "<li class='am-active' id = 'page_header"+i+this.idSuffix+"'><a href='javascript:'>"+i+"</a></li>";
                }else if(c.totalPage >= 5 && (c.totalPage - current) >= 5 && current < i && i < current+5){
                    html += "<li id = 'page_header"+i + this.idSuffix +"'><a href='javascript:'>"+i+"</a></li>";
                }else if(c.totalPage >= 5 && (c.totalPage - current) < 5 &&i > total - 5){
                    html += "<li id = 'page_header"+i+this.idSuffix +"'><a href='javascript:'>"+i+"</a></li>";
                }else if(c.totalPage < 5){
                    html += "<li id = 'page_header"+i+this.idSuffix +"'><a href='javascript:'>"+i+"</a></li>";
                }else{
                    html += "<li id = 'page_header"+i+this.idSuffix +"' style = 'display: none;'><a href='javascript:'>"+i+"</a></li>";
                }
            }
            //"<li><a href='#'>2</a></li>"+
            //"<li><a href='#'>3</a></li>"+
            //html += "<li id = 'next_page" + this.idSuffix + "'><a href='javascript:'>&raquo;</a></li></ul>";
            html += "<li id = 'next_page" + this.idSuffix + "'><a href='javascript:'>&raquo;</a></li>";
            html += "<li id = 'count"+this.idSuffix+"' class=\"am-disabled display:none\" ></span></li></ul>";
            //$(".main-view").append(html);
            $("#pagination"+ this.idSuffix)[0].innerHTML = html;
            if(c.totalPage == 1)$("#next_page"+ this.idSuffix).addClass("am-disabled");
        },
        allSelected: function(){
            if($("input[type='checkbox']").prop("checked") == true){
                $("input[type='checkbox']").prop("checked",true);
            }else {
                $("input[type='checkbox']").prop("checked",false);
            }
        },
        listDelete: function(deleteData){
            var idArray = [],params = {};
            for(var i = 0;i < deleteData.checkObj.length;i++){
                if(deleteData.checkObj[i].checked == true){
                    idArray.push(deleteData.checkObj[i].parentNode.parentNode.id);
                }
            }
            if(idArray.length == 0){alert("请选择删除列表项！");return;}else {
                if(!confirm("确认要删除吗？"))return;
            }
            params[deleteData.key] = idArray;
            c.data.ajax.post(deleteData.url, params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(deleteData.success)deleteData.success(data);
            }).error(function (err) {
                if(deleteData.error)deleteData.error(err);
            });
        },
        listAdd: function(addData){
            var params = addData.addData;
            c.data.ajax.post(addData.url, params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(addData.success)addData.success(data);
            }).error(function (err) {
                if(addData.error)addData.error(err);
            });
        },
        listUpdate: function(updateData){
            var params = updateData.updateData;
            c.data.ajax.post(updateData.url, params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(updateData.success)updateData.success(data);
            }).error(function (err) {
                if(updateData.error)updateData.error(err);
            });
        },
        evt: function(){//事件

            $(document).off("click","#page_header" + this.idSuffix +" li");
            $(document).off("click","#first_page"+ this.idSuffix);
            $(document).off("click","#pre_page"+ this.idSuffix);
            $(document).off("click","#next_page"+ this.idSuffix);

            $(document).on("click","#page_header" + this.idSuffix +" li",function(){
                if(Number(this.innerText)){
                    //cPage = Number(this.innerText);
                    c.selectPage(Number(this.innerText));
                    $(this).siblings().removeClass("am-active");
                    this.className = "am-active";
                    if(Number(this.innerText) != 1){
                        $("#pre_page"+ this.idSuffix).removeClass("am-disabled");
                    }else{
                        $("#pre_page"+ this.idSuffix).addClass("am-disabled");
                    }
                    if(Number(this.innerText) != c.totalPage)$("#next_page"+ this.idSuffix).removeClass("am-disabled"); else $("#next_page"+ this.idSuffix).addClass("am-disabled");
                }
            });
            $(document).on("click","#first_page"+ this.idSuffix,function(){
                for(var i = 1;i <= c.totalPage;i++){
                    if(i <= 5){
                        $("#page_header"+i+ this.idSuffix).css("display","inline-block");
                    }else{
                        $("#page_header"+i+ this.idSuffix).css("display","none");
                    }
                }
                $("#next_page"+ this.idSuffix).removeClass("am-disabled");
                cPage = 1;
                c.selectPage(1);
                var first = $(this).next().next();
                first.addClass("am-active");
                first.siblings().removeClass("am-active");
                $("#pre_page"+ this.idSuffix).addClass("am-disabled");
            });
            $(document).on("click","#pre_page"+ this.idSuffix,function(){
                var i = Number($(".am-active")[0].id.substring(11));
                if(cPage > 1){
                    $("#page_header"+(cPage-1)+ this.idSuffix).css("display","inline-block");
                    cPage--;
                    $("#page_header"+(cPage+5)+ this.idSuffix).css("display","none");
                }
                if(i==1){
                    return;
                }else if(i == c.totalPage){
                    $("#next_page"+ this.idSuffix).removeClass("am-disabled");
                }else if(i==2){$("#pre_page"+ this.idSuffix).addClass("am-disabled");}
                $("#page_header"+i+ this.idSuffix).removeClass("am-active");
                $("#page_header"+(i-1)+ this.idSuffix).addClass("am-active");
                c.selectPage(i-1);
            });
            $(document).on("click","#next_page"+ this.idSuffix,function(){
                if(c.totalPage == 1)return;
                var i = Number($(".am-active")[0].id.substring(11));
                if(cPage + 4 < c.totalPage && i > cPage + 3){
                    $("#page_header"+cPage+ this.idSuffix).css("display","none");
                    cPage++;
                    $("#page_header"+(cPage+4)+ this.idSuffix).css("display","inline-block");
                }
                if(i==1){
                    $("#pre_page"+ this.idSuffix).removeClass("am-disabled");
                }else if(i == c.totalPage){
                    return;
                }else if(i == c.totalPage-1){$("#next_page"+ this.idSuffix).addClass("am-disabled");}
                $("#page_header"+i+ this.idSuffix).removeClass("am-active");
                $("#page_header"+(i+1)+ this.idSuffix).addClass("am-active");
                c.selectPage(i+1);
            });
        }
    };
    w.pagination = pagination;
}(window));
